package com.junzhiit.nengji.cms.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.util.DBKey;
import com.junzhiit.nengji.bo.TableNumberBO;
import com.junzhiit.nengji.cms.service.TableNumberService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.TableNumberNotFoundException;
import com.junzhiit.nengji.cms.web.exception.WechatConfigNotFoundException;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.util.wechat.WeChatUtils;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.TableNumberAddVO;
import com.junzhiit.nengji.cms.web.vo.TableNumberBatchAddVO;
import com.junzhiit.nengji.cms.web.vo.TableNumberEditVO;
import com.junzhiit.nengji.cms.web.vo.TableNumberVO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.TableNumberDAO;
import com.junzhiit.nengji.dao.WechatConfigDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.TableNumber;
import com.junzhiit.nengji.model.WechatConfig;

@Service
public class TableNumberServiceImpl extends BaseServiceImpl implements TableNumberService{

	@Autowired
	private TableNumberDAO tableNumberDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private WechatConfigDAO wechatConfigDAO;
	
	@Value("${oss.backet.name}")
	private String backetName;
	
	@Value("${upload.mall.shop.table.qrcode}")
	private String tableQrcodePath;
	
	@Override
	public Map<String, Object> findPageAll(DataTablesParamVO dtp,TableNumberVO tableNumberVO) {
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(tableNumberVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		TableNumber tableNumber=new TableNumber();
		BeanUtils.copyProperties(tableNumberVO, tableNumber);
		tableNumber.setShopId(shop.getId());
		
		
		List<TableNumberBO> list = tableNumberDAO.findPageAll(tableNumber, dtp.getStart(), dtp.getLength());
		int totalCount = tableNumberDAO.getTotalCount(tableNumber);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Transactional
	@Override
	public ResultVO add(TableNumberAddVO tableNumberAddVO) {
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(tableNumberAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		WechatConfig wechatConfig=wechatConfigDAO.getWechatConfigByCsId(shop.getId());
		if(wechatConfig==null){
			throw new WechatConfigNotFoundException("微信配置未找到");
		}
		
		String qrCode=getQrCode(shop.getId(),tableNumberAddVO.getCode(),wechatConfig);
		
		int maxSort=tableNumberDAO.getMaxSort(shop.getId());
		
		TableNumber tableNumber=new TableNumber();
		BeanUtils.copyProperties(tableNumberAddVO, tableNumber);
		tableNumber.setShopId(shop.getId());
		tableNumber.setQrCode(qrCode);
		tableNumber.setSort(maxSort+Constants.ONE);
		tableNumberDAO.insert(tableNumber);
		
		return result;
	}
	
	@Transactional
	@Override
	public ResultVO batchAdd(TableNumberBatchAddVO tableNumberBatchAddVO) {
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(tableNumberBatchAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		List<TableNumber> list=new ArrayList<TableNumber>();
		WechatConfig wechatConfig=wechatConfigDAO.getWechatConfigByCsId(shop.getId());
		if(wechatConfig==null){
			throw new WechatConfigNotFoundException("请先配置微信配置");
		}
		
		int maxSort=tableNumberDAO.getMaxSort(shop.getId());
		
		for(int i=tableNumberBatchAddVO.getStart();i<=tableNumberBatchAddVO.getEnd();i++){
			
			TableNumber tableNumber=new TableNumber();
			String prevName=tableNumberBatchAddVO.getPrevName()==null?"":tableNumberBatchAddVO.getPrevName();
			String code=prevName+i;
			//qrcode
			String qrCode=getQrCode(shop.getId(),code,wechatConfig);
			
			tableNumber.setCode(code);
			tableNumber.setShopId(shop.getId());
			tableNumber.setQrCode(qrCode);
			tableNumber.setTableTypeId(tableNumberBatchAddVO.getTableTypeId());
			tableNumber.setSort(maxSort+i);
			list.add(tableNumber);
		}
		
		tableNumberDAO.batchInsert(list);
		return result;
	}
	
	@Override
	public ResultVO edit(TableNumberEditVO tableNumberEditVO) {
		ResultVO result=new ResultVO();
		
		TableNumber data=tableNumberDAO.query(tableNumberEditVO.getId());
		if(data==null){
			throw new TableNumberNotFoundException("桌号未找到");
		}
		
		TableNumber tableNumber=new TableNumber();
		BeanUtils.copyProperties(tableNumberEditVO, tableNumber);
		tableNumberDAO.update(tableNumber);
		
		return result;
	}
	
	@Override
	public ResultVO regenerate(String id){
		ResultVO result=new ResultVO();
		TableNumber tableNumber=tableNumberDAO.query(id);
		if(tableNumber==null){
			throw new TableNumberNotFoundException("桌号未找到");
		}
		
		AliyunOSSClientUtil.removeFile(backetName, tableQrcodePath+tableNumber.getQrCode());
		WechatConfig wechatConfig=wechatConfigDAO.getWechatConfigByCsId(tableNumber.getShopId());
		String qrCode=getQrCode(tableNumber.getShopId(),tableNumber.getCode(),wechatConfig);
		tableNumber.setQrCode(qrCode);
		tableNumberDAO.update(tableNumber);
		
		return result;
	}

	/**
	 * 
	 * TODO 生成二维码 返回二维码路径
	 * @author FEB
	 * @time 2018年9月19日 上午9:45:31
	 * @return String
	 */
	private String getQrCode(String shopId,String tableNumber,WechatConfig wechatConfig){
		if(wechatConfig==null){
			throw new WechatConfigNotFoundException("微信配置未找到");
		}
		Map<String, String> param=new HashMap<String, String>();
		param.put(Constants.SHOP_ID,shopId);
		String code=null;
		try {
			code = URLEncoder.encode(tableNumber, Constants.GBK);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		param.put(Constants.CODE,code);
		String qrCode=DBKey.getUUID()+".png";
		WeChatUtils.wxQRCode(wechatConfig, backetName, tableQrcodePath, qrCode, Constants.PARISH_FOOD_URL, param);
		
		return qrCode;
	}
	
	@Override
	public ResultVO updateToDelete(String id){
		ResultVO result=new ResultVO();
		TableNumber tableNumber=tableNumberDAO.query(id);
		if(tableNumber==null){
			throw new TableNumberNotFoundException("桌号未找到");
		}
		
		tableNumberDAO.updateToDelete(id);
		
		return result;
	}
}
